package com.example.csmall.product.service.impl;

import com.example.csmall.product.ex.ServiceException;
import com.example.csmall.product.mapper.AttributeMapper;
import com.example.csmall.product.pojo.dto.AttributeAddNewDTO;
import com.example.csmall.product.pojo.dto.AttributeUpdateDTO;
import com.example.csmall.product.pojo.entity.Attribute;
import com.example.csmall.product.pojo.vo.AlbumStandardVO;
import com.example.csmall.product.pojo.vo.AttributeListByTemplateIdVO;
import com.example.csmall.product.pojo.vo.AttributeListItemVO;
import com.example.csmall.product.pojo.vo.AttributeStandardVO;
import com.example.csmall.product.service.AttributeService;
import com.example.csmall.product.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttributeServiceImpl implements AttributeService {

    @Autowired
    AttributeMapper attributeMapper;

    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO) {
        int countName = attributeMapper.countName(attributeAddNewDTO.getName());
        if(countName>0){
            String message = "您输入的模板名"+attributeAddNewDTO.getName()+"已经存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO,attribute);
        attributeMapper.insert(attribute);
    }

    @Override
    public void delete(Long id) {
        AttributeStandardVO standarById = attributeMapper.getStandarById(id);
        if(standarById==null){
            String message="删除数据失败，数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = attributeMapper.deleteById(id);
        if(rows!=1){
            String message="删除数据失败，服务器正忙，请稍后重试!";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, AttributeUpdateDTO attributeUpdateDTO) {
        AttributeStandardVO standarById = attributeMapper.getStandarById(id);
        if(standarById==null){
            String message="修改数据失败，数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO,attribute);
        attribute.setId(id);
        int rows = attributeMapper.updateById(attribute);
        if(rows!=1){
            String message="修改属性详情失败，服务器正忙，请稍后重试!";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public List<AttributeListItemVO> list() {
        List<AttributeListItemVO> list = attributeMapper.list();
        return list;
    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        AttributeStandardVO standarById = attributeMapper.getStandarById(id);
        if(standarById == null){
            String message = "没有查询到对应的相册数据";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standarById;
    }

    @Override
    public List<AttributeListByTemplateIdVO> listByTemplateId(Long templateId) {
        List<AttributeListByTemplateIdVO> list = attributeMapper.listByTemplateId(templateId);
        return list;
    }

}
